package com.yunyike.app.service;


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.rop.annotation.IgnoreSignType;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.Response;
import com.yunyike.app.constants.CommonConstant;
import com.yunyike.app.data.model.Shop;
import com.yunyike.app.data.model.Shopper;
import com.yunyike.app.data.model.User;
import com.yunyike.app.data.service.IShopService;
import com.yunyike.app.data.service.IShopperService;
import com.yunyike.app.data.service.IUserService;
import com.yunyike.app.redis.JedisManager;
import com.yunyike.app.request.ConnectRequest;
import com.yunyike.app.request.LogonRequest;
import com.yunyike.app.request.ShopperRequest;
import com.yunyike.app.response.ShopperSessionResponse;

@ServiceMethodBean(ignoreSign = IgnoreSignType.NO)
public class APPShopperService {

    private Logger log = LoggerFactory.getLogger(APPShopperService.class);
    
    @Autowired
	private IShopperService iShopperService;
    @Autowired
   	private IShopService iShopService;
    @Autowired
   	private  IUserService userService;
    
    
	@ServiceMethod(method = "shopper.logon", version = "1.0", needInSession = NeedInSessionType.NO)
	public Response logon(LogonRequest logonRequest) {

		Response response = new Response();
		String userName = logonRequest.getUserName();
		String password = logonRequest.getPassword();
		String msg = "";
		// String decoder = URLDecoder.decode(loginRequest.getPassword(),
		// "UTF-8");
		// String password = AESUtil.decrypt(decoder);
		log.info("shopper--logon::username=" + userName + ",password=" + password);
		try {
			Shopper shopper = iShopperService.shopperLogon(userName, password);

			if (shopper == null) {
				msg = "未获取到用户信息";
				response.getResult().setResult("0");
			} else {
				msg = "登录成功";
				response.getResult().setResult("1");
				String sessionId = UUID.randomUUID().toString();
				ShopperSessionResponse shopperSessionResponse = new ShopperSessionResponse();
				this.copyProperties(shopperSessionResponse, shopper);
				JedisManager.setObject(sessionId, CommonConstant.SHOPPER_KEY_ET, shopperSessionResponse);
				Map<String, String> data = new HashMap<String, String>(1);
				data.put("sessionId", sessionId);
				data.put("shopperId", shopper.getShopperId());
				data.put("shopId", shopper.getShopId());
				response.setData(data);
				 
			}
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("shopper--logon::msg=" + msg);
		return response;
	}

	private void copyProperties( ShopperSessionResponse sessionReponse ,Shopper shopper){
		sessionReponse.setShopperId(shopper.getShopperId());
		sessionReponse.setShopId(shopper.getShopId());
		sessionReponse.setShopperBankNumber(shopper.getShopperBankNumber());
		sessionReponse.setShopperCommisionId(shopper.getShopperCommisionId());
		sessionReponse.setShopperCtime(shopper.getShopperCtime());
		sessionReponse.setShopperEmail(shopper.getShopperEmail());
		sessionReponse.setShopperPhoto(shopper.getShopperPhoto());
		sessionReponse.setShopperTel(shopper.getShopperTel());
	}
	@ServiceMethod(method = "shopper.connect", version = "1.0", needInSession = NeedInSessionType.NO)
	public Response testConnectUser(ConnectRequest connectRequest) {
		Response response = new Response();
		String shopperId = connectRequest.getShopperId();
		String userId = connectRequest.getUserId();
		String msg = "";
		log.info("shopper--logon::shopperId=" + shopperId + ",userId=" + userId);
		try {
			Shopper shopper = iShopperService.findShopperById(shopperId);
			User user = userService.findUserById(userId);
			if (shopper == null||user==null) {
				msg = "未获取到用户或导购";
				response.getResult().setResult("0");
			}else{
				JedisManager.setObject(shopperId+CommonConstant.SHOPPER_CONNECT_USER, CommonConstant.SHOPPER_KEY_ET, user);
				JedisManager.setObject(userId+CommonConstant.SHOPPER_CONNECT_USER, CommonConstant.SHOPPER_KEY_ET, shopper);
				msg = "模拟导购链接用户成功";
				response.getResult().setResult("1");
			}
		}catch(Exception e){
			e.printStackTrace();
			msg = "模拟导购链接用户失败";
			response.getResult().setResult("0");
		}
		response.getResult().setMessage(msg);
		return response;
	}
	@ServiceMethod(method = "shopper.get", version = "1.0", needInSession = NeedInSessionType.YES)
	public Response getShopper(ShopperRequest shopperRequest) {

		Response response = new Response();
		String shopperId = shopperRequest.getShopperId();
//		ShopperSessionResponse session = (ShopperSessionResponse) JedisManager.getObject(
//				shopperRequest.getRopRequestContext().getSessionId(),ShopperSessionResponse.class);
		
		String msg = "";
		log.info("shopper--getShopper::shopperId=" + shopperId);
		try {
			Shop shop = null;
			Shopper shopper = iShopperService.findShopperById(shopperId);
//			if (session == null) {
//				msg = "未获取到用户信息";
//				response.getResult().setResult("0");
//			} else {
				shop = iShopService.findShopById(shopper.getShopId());
				msg = "获取用户信息成功";
				response.getResult().setResult("1");
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("shopper", shopper);
				data.put("shop", shop);
				response.setData(data);
//			}
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("shopper--logon::msg=" + msg);
		return response;
	}
	
}

